<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-adx__accountreportday">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      

       <el-form-item label="投放起始时间">
          <el-date-picker v-model="dataForm.stime" value-format="yyyy-MM-dd" type="date"
            placeholder="投放起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="投放结束时间">
          <el-date-picker v-model="dataForm.etime" value-format="yyyy-MM-dd" type="date"
            placeholder="投放结束时间">
          </el-date-picker>
        </el-form-item>
         
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
      </el-form>
      </el-form>
      <el-table :span-method="objectSpanMethod" v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;" 
       show-summary :summary-method="getSummaries"     >
        <el-table-column type="selection"  header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="day" label="日期" header-align="center" align="center"></el-table-column>
        <el-table-column prop="productAmout"  label="总收益" header-align="center" align="center"></el-table-column>
        <el-table-column prop="mediaAmout"  label="总消耗" header-align="center" align="center"></el-table-column>
        <el-table-column prop="profit" label="总利润" header-align="center" align="center"></el-table-column>
        <el-table-column prop="actEventTypeTotal" label="转换总数" header-align="center" align="center"></el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './accountreportday-add-or-update'
import { getDictDataList } from '@/utils'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/adx/accountreportday/day/dayTotalPage',
        getDataListIsPage: true,
        exportURL: '/adx/accountreportday/export',
        deleteURL: '/adx/accountreportday',
        deleteIsBatch: true
      },
      userNameList: [],
      businessList:[],
      mediaList:[],
      productList:[],
      exportURL:window.SITE_CONFIG['apiURL']+'/adx/accountreportday/import',
      dataForm: {
        id: ''
      }
    }
  },
  mounted() {
    this.getNameDict();
    this.getBusinessDict();
    this.getProductList();
    this.getMediaList();
  },
  components: {
    AddOrUpdate
  },
    methods:{
    getNameDict() {
        this.userNameList = getDictDataList("userNameList")
    },
    
   download() {
       window.open(window.SITE_CONFIG['apiURL']+'/adx/accountreportday/download')
    },
    getBusinessDict() {
      this.businessList = getDictDataList("businessList")
    },
           getMediaList() {
      this.mediaList = getDictDataList("mediaList")
    },
    getProductList() {
      this.productList = getDictDataList("productList")
    },
  getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
      if (index === 1) {
        sums[index] = '数据合计';
        return;
      }
      const values = data.map(item => Number(item[column.property]));
      // 只对amount这一列进行总计核算。
      if (column.property === 'profit'|| column.property === 'productAmout'|| column.property === 'mediaAmout'|| column.property === 'eventTypeTotal') {
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
          const value =  Number(curr);
          if (!isNaN(value)) {
           return  Math.floor((prev + curr)*100)/100 ;
          }else {
           return prev;
          }
          }, 0);
           sums[index] += '';
        } else {
         sums[index] = '---'
        }
      }
      });
      return sums;
    },
     objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === this.dataList.length) {
        console.log(1111111111)
        if (columnIndex === 0) {
          return {
            rowspan: 1,
            colspan: 2
          };
        } else if (columnIndex === 1) {
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    }
  }
}
</script>
